#include <stdio.h>
#include <pthread.h>

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *thread_func(void *arg)
{
    int *p = (arg);
    pthread_mutex_lock(&mutex);
    *p += 1;
    pthread_mutex_unlock(&mutex);
    return NULL;
}


int main()
{
    int num = 0;
    int thread_num = 20000;
    pthread_t tid[thread_num];
    for (int i = 0; i < thread_num; i++)
    {
        pthread_create(&tid[i], NULL, thread_func, &num);
    }
    for (int i = 0; i < thread_num; i++)
    {
        pthread_join(tid[i], NULL);
    }
    printf("num = %d\n", num);
    pthread_mutex_destroy(&mutex);
    return 0;
}